package com.housediyadmin.service
{
	import com.housediyadmin.events.buildingEvents.GetBuildingEvent;
	import com.housediyadmin.events.houseEvents.GetHousesEvent;
	import com.housediyadmin.events.treeEvents.GetAreaTreeEvent;
	import com.housediyadmin.vo.Building;
	import com.housediyadmin.vo.House;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/11/27
	 */	
	public class BuildingRemoteObjectService extends Actor
	{
		public var  buildingRemoteObject:RemoteObject;
		

		public function BuildingRemoteObjectService()
		{
			super();
			
			var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediyadmin/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://10.60.1.120/HouseDiyAdmin/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			buildingRemoteObject = new RemoteObject("fluorine");
			buildingRemoteObject.source = "HouseDiyService.HouseDiyAction";
			buildingRemoteObject.showBusyCursor = true;
			buildingRemoteObject.channelSet = channelSet;
			
			/*区域层级操作*/
			/*buildingRemoteObject.getOperation("getAreaLevel").addEventListener(ResultEvent.RESULT, getAreaTreeResult);
			buildingRemoteObject.getOperation("getAreaLevel").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("addAreaLevelType").addEventListener(ResultEvent.RESULT, addAreaLevelResult);
			buildingRemoteObject.getOperation("addAreaLevelType").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("editAreaLevelType").addEventListener(ResultEvent.RESULT, editAreaLevelResult);
			buildingRemoteObject.getOperation("editAreaLevelType").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("deleteAreaLevelType").addEventListener(ResultEvent.RESULT, deleteAreaLevelResult);
			buildingRemoteObject.getOperation("deleteAreaLevelType").addEventListener(FaultEvent.FAULT, faultHandler);
			*/
			/*buildingRemoteObject.getOperation("addAreaLevelBuilding").addEventListener(ResultEvent.RESULT, addBuildingLevelResult);
			buildingRemoteObject.getOperation("addAreaLevelBuilding").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("editAreaLevelBuilding").addEventListener(ResultEvent.RESULT, editBuildingLevelResult);
			buildingRemoteObject.getOperation("editAreaLevelBuilding").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("deleteAreaLevelBuilding").addEventListener(ResultEvent.RESULT, deleteBuildingLevelResult);
			buildingRemoteObject.getOperation("deleteAreaLevelBuilding").addEventListener(FaultEvent.FAULT, faultHandler);*/
		    
			/*楼盘操作*/
			/*buildingRemoteObject.getOperation("getBuildingIDsFromAreas").addEventListener(ResultEvent.RESULT, getBuilidngIdsResult);
			buildingRemoteObject.getOperation("getBuildingIDsFromAreas").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("getRecordsBuilding").addEventListener(ResultEvent.RESULT, getBuildingsResult);
			buildingRemoteObject.getOperation("getRecordsBuilding").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("addBuilding").addEventListener(ResultEvent.RESULT, addBuildingResult);
			buildingRemoteObject.getOperation("addBuilding").addEventListener(FaultEvent.FAULT, faultHandler);						
			
		    buildingRemoteObject.getOperation("deleteBuildings").addEventListener(ResultEvent.RESULT, deleteBuildingResult);
			buildingRemoteObject.getOperation("deleteBuildings").addEventListener(FaultEvent.FAULT, faultHandler);
		    
			buildingRemoteObject.getOperation("editBuilding").addEventListener(ResultEvent.RESULT, editBuildingResult);
			buildingRemoteObject.getOperation("editBuilding").addEventListener(FaultEvent.FAULT, faultHandler);
			
			/*户型操作*/
			/*buildingRemoteObject.getOperation("getHouseIDsFromBuildings").addEventListener(ResultEvent.RESULT, getHouseIdsByBuildingId);
			buildingRemoteObject.getOperation("getHouseIDsFromBuildings").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("getRecordsHouse").addEventListener(ResultEvent.RESULT, getHouseListResult);
			buildingRemoteObject.getOperation("getRecordsHouse").addEventListener(FaultEvent.FAULT, faultHandler);
					
			buildingRemoteObject.getOperation("addHouse").addEventListener(ResultEvent.RESULT, addHouseResult);
			buildingRemoteObject.getOperation("addHouse").addEventListener(FaultEvent.FAULT, faultHandler);
	
			buildingRemoteObject.getOperation("editHouse").addEventListener(ResultEvent.RESULT, editHouseResult);
			buildingRemoteObject.getOperation("editHouse").addEventListener(FaultEvent.FAULT, faultHandler);
			
			buildingRemoteObject.getOperation("deleteHouses").addEventListener(ResultEvent.RESULT, deleteHouseResult);
			buildingRemoteObject.getOperation("deleteHouses").addEventListener(FaultEvent.FAULT, faultHandler);	*/								
		}
		
		
		public function getAreaTree():void
		{
			buildingRemoteObject.getAreaLevel();
		}
		
		public function addAreaLevel(typeName:String, typeParentId:int, typeLevel:int):void
		{
			buildingRemoteObject.addAreaLevelType(typeName, typeParentId, typeLevel);
		}
		
		public function editAreaLevel(typeId:int, typeName:String):void
		{
			buildingRemoteObject.editAreaLevelType(typeId, typeName);
		}
		
		public function deleteAreaLevel(typeId:int):void
		{
			buildingRemoteObject.deleteAreaLevelType(typeId);
		}
		
		public function addBuildingLevel(buildingName:String, areaId:int):void
		{
			buildingRemoteObject.addAreaLevelBuilding(buildingName, areaId);
		}
		
		public function editBuildingLevel(buildingId:int, buildingName:String):void
		{
			buildingRemoteObject.editAreaLevelBuilding(buildingId, buildingName);
		}
		
		public function deleteBuildingLevel(buildingId:int):void
		{
			buildingRemoteObject.deleteAreaLevelBuilding(buildingId);
		}
		
		/**
		 * 通过一串区域id得到属于它的楼盘id串
		 * @param event
		 * 
		 */	
		public function getBuildingIds(areaIds:ArrayCollection):void
		{
		     buildingRemoteObject.getBuildingIDsFromAreas(areaIds);	
		}
		
		/**
		 *根据一串buildingId得到相对应的building记录 
		 * @param buildingIds：ArrayCollection
		 * 
		 */		
		public function getBuildings(buildingIds:ArrayCollection = null):void
		{
			buildingRemoteObject.getRecordsBuilding(buildingIds);
		}
		
		/**
		 *选中区域树上的某一节点，添加一个楼盘，返回该节点下所有的楼盘 
		 * @param building
		 * @param areaIds
		 * 
		 */			
		public function addBuildingDetail(building:Building, areaIds:Array):void
		{
			buildingRemoteObject.addBuilding(building, areaIds);
		}
		
		
		public function deleteBuilding(buildingIds:Array, areaIds:Array):void
		{
			buildingRemoteObject.deleteBuildings(buildingIds, areaIds);
		}
		
		public function editOneBuilding(building:Building, areaIds:Array):void
		{
			buildingRemoteObject.editBuilding(building, areaIds);
		}
		
		/**
		 * 添加一个户型
		 * @param house:House
		 * 
		 */		
		public function addOneHouse(house:House):void
		{
			buildingRemoteObject.addHouse(house);
		}
		
		/**
		 * 通过一串或一个楼盘id得到属于它的房型id串
		 * @param buildingId
		 * 
		 */		
		public function getHouseIds(buildingIds:ArrayCollection):void
		{
			buildingRemoteObject.getHouseIDsFromBuildings(buildingIds);
		}
		
		public function getHouseList(buildingIds:ArrayCollection):void
		{
			buildingRemoteObject.getRecordsHouse(buildingIds);
		}
		
		public function editOneHouse(house:House):void
		{
			buildingRemoteObject.editHouse(house);
		}
		
		public function deleteHousesByIds(houseIds:Array, buildingId:int):void
		{
			buildingRemoteObject.deleteHouses(houseIds, buildingId);
		}
		
		private function getAreaTreeResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		private function  addAreaLevelResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		private function editAreaLevelResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		private function deleteAreaLevelResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		private function addBuildingLevelResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		private function editBuildingLevelResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		private function deleteBuildingLevelResult(event:ResultEvent):void
		{
			dispatch(new GetAreaTreeEvent(GetAreaTreeEvent.UPDATE_AREA_TREE, new XMLListCollection((event.result as XML).children())));
		}
		
		/**
		 * 得到楼盘Id的集合
		 * @param event
		 * 
		 */		
		private function getBuilidngIdsResult(event:ResultEvent):void
		{
			if(event.result as ArrayCollection == null)
			{
				dispatch(new GetBuildingEvent(GetBuildingEvent.UPDATE_BUILDING_MODEL, null, null, null));
			}
			else{
				dispatch(new GetBuildingEvent(GetBuildingEvent.GET_BUILDINGS_BY_IDS, null, event.result as ArrayCollection , null));
			}			
		}
		
		private function addBuildingResult(event:ResultEvent):void
		{
		   dispatch(new GetBuildingEvent(GetBuildingEvent.UPDATE_BUILDING_MODEL, null, null, event.result as ArrayCollection));
		}
		
		/**
		 *更新model里面的数据 
		 * @param event
		 * 
		 */		
		private function getBuildingsResult(event:ResultEvent):void
		{
			dispatch(new GetBuildingEvent(GetBuildingEvent.UPDATE_BUILDING_MODEL, null, null, event.result as ArrayCollection));
		}
		
		private function editBuildingResult(event:ResultEvent):void
		{
			dispatch(new GetBuildingEvent(GetBuildingEvent.UPDATE_BUILDING_MODEL, null, null, event.result as ArrayCollection));
		}
		
		private function deleteBuildingResult(event:ResultEvent):void
		{
			dispatch(new GetBuildingEvent(GetBuildingEvent.UPDATE_BUILDING_MODEL, null, null, event.result as ArrayCollection));			
		}

		//户型相关操作
		private function addHouseResult(event:ResultEvent):void
		{
			dispatch(new GetHousesEvent(GetHousesEvent.UPDATE_HOUSE_MODEL, null, null, event.result as ArrayCollection));
		}
		
		/**
		 * 通过一串或一个buildingid得到它下面所有的户型的Id(HouseId)
		 * @param event
		 * 
		 */		
		private function getHouseIdsByBuildingId(event:ResultEvent):void
		{
			if(event.result as ArrayCollection == null)
			{
				dispatch(new GetHousesEvent(GetHousesEvent.UPDATE_HOUSE_MODEL, null, null, null));
			}
			else
			{
				dispatch(new GetHousesEvent(GetHousesEvent.GET_HOUSES_BY_IDS, null, event.result as ArrayCollection, null));
			}
		}
		
		/**
		 *通过HouseId集合得到HouseList 
		 * @param event
		 * 
		 */		
		private function getHouseListResult(event:ResultEvent):void
		{		
			dispatch(new GetHousesEvent(GetHousesEvent.UPDATE_HOUSE_MODEL, null, null, event.result as ArrayCollection));
		}
		
		private function editHouseResult(event:ResultEvent):void
		{
			dispatch(new GetHousesEvent(GetHousesEvent.UPDATE_HOUSE_MODEL, null, null, event.result as ArrayCollection));
		}
		
		private function deleteHouseResult(event:ResultEvent):void
		{

			dispatch(new GetHousesEvent(GetHousesEvent.UPDATE_HOUSE_MODEL, null, null, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}